using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace CKY.AgentPlatform.Application.Contracts.McpTools.Dtos
{
    /// <summary>
    /// MCP工具执行输入DTO
    /// </summary>
    public class ExecuteMcpToolInput
    {
        /// <summary>
        /// 工具参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();

        /// <summary>
        /// 执行超时时间（毫秒）
        /// </summary>
        public int Timeout { get; set; } = 30000;

        /// <summary>
        /// 是否异步执行
        /// </summary>
        public bool Async { get; set; } = false;
    }

    /// <summary>
    /// MCP工具执行结果DTO
    /// </summary>
    public class McpToolExecutionResultDto
    {
        /// <summary>
        /// 执行是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 执行结果
        /// </summary>
        public object Result { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string Error { get; set; }

        /// <summary>
        /// 执行时间（毫秒）
        /// </summary>
        public long ExecutionTime { get; set; }

        /// <summary>
        /// 执行时间戳
        /// </summary>
        public DateTime ExecutedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// MCP工具测试结果DTO
    /// </summary>
    public class McpToolTestResultDto
    {
        /// <summary>
        /// 测试是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 测试结果消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 测试时间戳
        /// </summary>
        public DateTime TestedAt { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// MCP工具统计信息DTO
    /// </summary>
    public class McpToolStatisticsDto
    {
        /// <summary>
        /// 总工具数量
        /// </summary>
        public int TotalTools { get; set; }

        /// <summary>
        /// 活跃工具数量
        /// </summary>
        public int ActiveTools { get; set; }

        /// <summary>
        /// 总执行次数
        /// </summary>
        public long TotalExecutions { get; set; }

        /// <summary>
        /// 成功执行次数
        /// </summary>
        public long SuccessfulExecutions { get; set; }

        /// <summary>
        /// 失败执行次数
        /// </summary>
        public long FailedExecutions { get; set; }

        /// <summary>
        /// 平均执行时间（毫秒）
        /// </summary>
        public double AverageExecutionTime { get; set; }

        /// <summary>
        /// 成功率
        /// </summary>
        public double SuccessRate { get; set; }

        /// <summary>
        /// 统计时间戳
        /// </summary>
        public DateTime StatisticsTimestamp { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// MCP工具状态枚举
    /// </summary>
    public enum McpToolStatus
    {
        /// <summary>
        /// 活跃
        /// </summary>
        Active = 1,

        /// <summary>
        /// 停用
        /// </summary>
        Inactive = 2,

        /// <summary>
        /// 维护中
        /// </summary>
        Maintenance = 3
    }

    /// <summary>
    /// MCP工具DTO
    /// </summary>
    public class McpToolDto : EntityDto<Guid>
    {
        /// <summary>
        /// 工具名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 工具描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 工具分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 工具状态
        /// </summary>
        public McpToolStatus Status { get; set; }

        /// <summary>
        /// 是否激活
        /// </summary>
        public bool IsActive { get; set; }

        /// <summary>
        /// 工具版本
        /// </summary>
        public string Version { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; set; }

        /// <summary>
        /// 执行次数
        /// </summary>
        public long ExecutionCount { get; set; }

        /// <summary>
        /// 成功次数
        /// </summary>
        public long SuccessCount { get; set; }

        /// <summary>
        /// 失败次数
        /// </summary>
        public long FailureCount { get; set; }

        /// <summary>
        /// 平均执行时间
        /// </summary>
        public double AverageExecutionTime { get; set; }

        /// <summary>
        /// 最后执行时间
        /// </summary>
        public DateTime? LastExecutedAt { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }
    }

    /// <summary>
    /// MCP工具详细信息DTO
    /// </summary>
    public class McpToolDetailDto : McpToolDto
    {
        /// <summary>
        /// 工具配置
        /// </summary>
        public Dictionary<string, object> Configuration { get; set; } = new();

        /// <summary>
        /// 工具参数定义
        /// </summary>
        public List<ParameterDefinition> Parameters { get; set; } = new();

        /// <summary>
        /// 工具返回值定义
        /// </summary>
        public ReturnValueDefinition ReturnValue { get; set; }

        /// <summary>
        /// 工具示例
        /// </summary>
        public List<ToolExample> Examples { get; set; } = new();
    }

    /// <summary>
    /// 参数定义
    /// </summary>
    public class ParameterDefinition
    {
        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 参数类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 参数描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 是否必需
        /// </summary>
        public bool Required { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public object DefaultValue { get; set; }
    }

    /// <summary>
    /// 返回值定义
    /// </summary>
    public class ReturnValueDefinition
    {
        /// <summary>
        /// 返回值类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 返回值描述
        /// </summary>
        public string Description { get; set; }
    }

    /// <summary>
    /// 工具示例
    /// </summary>
    public class ToolExample
    {
        /// <summary>
        /// 示例名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 示例描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 示例输入
        /// </summary>
        public Dictionary<string, object> Input { get; set; } = new();

        /// <summary>
        /// 示例输出
        /// </summary>
        public object Output { get; set; }
    }

  
    /// <summary>
    /// MCP工具类型枚举
    /// </summary>
    public enum McpToolType
    {
        /// <summary>
        /// 文件操作
        /// </summary>
        FileSystem = 0,

        /// <summary>
        /// 网络请求
        /// </summary>
        Http = 1,

        /// <summary>
        /// 数据库操作
        /// </summary>
        Database = 2,

        /// <summary>
        /// 代码执行
        /// </summary>
        CodeExecution = 3,

        /// <summary>
        /// AI模型调用
        /// </summary>
        AIModel = 4,

        /// <summary>
        /// 系统操作
        /// </summary>
        System = 5,

        /// <summary>
        /// 数据处理
        /// </summary>
        DataProcessing = 6,

        /// <summary>
        /// 消息通信
        /// </summary>
        Messaging = 7,

        /// <summary>
        /// 其他
        /// </summary>
        Other = 99
    }

    /// <summary>
    /// 获取MCP工具列表输入DTO
    /// </summary>
    public class GetMcpToolListInput : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 工具名称筛选
        /// </summary>
        public string? Filter { get; set; }

        /// <summary>
        /// 工具类型筛选
        /// </summary>
        public McpToolType? ToolType { get; set; }

        /// <summary>
        /// 分类筛选
        /// </summary>
        public string? Category { get; set; }

        /// <summary>
        /// 状态筛选
        /// </summary>
        public McpToolStatus? Status { get; set; }

        /// <summary>
        /// 是否激活筛选
        /// </summary>
        public bool? IsActive { get; set; }
    }

    /// <summary>
    /// 创建MCP工具输入DTO
    /// </summary>
    public class CreateMcpToolInput
    {
        /// <summary>
        /// 工具名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 工具描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 工具类型
        /// </summary>
        public McpToolType ToolType { get; set; }

        /// <summary>
        /// 工具分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 工具配置
        /// </summary>
        public Dictionary<string, object> Configuration { get; set; } = new();

        /// <summary>
        /// 工具版本
        /// </summary>
        public string Version { get; set; } = "1.0.0";

        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; set; }
    }

    /// <summary>
    /// 更新MCP工具输入DTO
    /// </summary>
    public class UpdateMcpToolInput
    {
        /// <summary>
        /// 工具名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 工具描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 工具分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 工具配置
        /// </summary>
        public Dictionary<string, object> Configuration { get; set; } = new();

        /// <summary>
        /// 工具版本
        /// </summary>
        public string Version { get; set; }

        /// <summary>
        /// 作者
        /// </summary>
        public string Author { get; set; }
    }

  }